//
//  GroupViewController.m
//  项目
//
//  Created by 张诗雨 on 15/1/6.
//  Copyright (c) 2015年 张诗雨. All rights reserved.
//

#import "GroupViewController.h"
#import "HotTopicTableViewCell.h"
#import "HotGroupTableViewCell.h"
#import "AFNetWoringYu.h"
#import "UIImage+GaussianBlur.h"
#import "HotTopicModel.h"
#import "WebColor.h"
#import "HotGroupModel.h"
@interface GroupViewController ()

@end

@implementation GroupViewController

- (void)dealloc
{
    [_hotTopicArray release];
    [_hotGroupArray release];
    [_groupTableView release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hotTopicArray = [NSMutableArray array];
        self.hotGroupArray = [NSMutableArray array];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *white = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:white];
    
    [white release];
    
    self.navigationController.navigationBar.translucent = YES;
    self.title = @"互动";
    
    [self startConnect];
    [self createView];
    // Do any additional setup after loading the view.
}


-(void)createView
{
    self.groupTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    [self.groupTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    _groupTableView.delegate = self;
    _groupTableView.dataSource = self;
    _groupTableView.backgroundColor = [WebColor whiteSmoke];
    [_groupTableView registerClass:[HotTopicTableViewCell class] forCellReuseIdentifier:@"firstGroupCell"];
    [_groupTableView registerClass:[HotGroupTableViewCell class] forCellReuseIdentifier:@"secondGroupCell"];
    [self.view addSubview:_groupTableView];
    [_groupTableView release];
    
    UIButton *returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    returnButton.frame = CGRectMake(0, 0, 25, 25);
    [returnButton setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui.png"] forState:UIControlStateNormal];
    [returnButton addTarget:self action:@selector(ReturnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:returnButton];
    
    
}
- (void)ReturnAction:(id)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *gray = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 40)];
        gray.backgroundColor = [WebColor whiteSmoke];
        
        UIImageView *hot = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, 20, 20)];
        hot.image = [UIImage imageNamed:@"hot.png"];
        [gray addSubview:hot];
        [hot release];
        
        UILabel *topic = [[UILabel alloc] initWithFrame:CGRectMake(30, 10, gray.frame.size.width / 4, 40)];
        topic.text = @"热门话题";
        topic.textColor = [WebColor steelBlue];
        topic.font = [UIFont boldSystemFontOfSize:16.0f];
        [gray addSubview:topic];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(self.view.frame.size.width - 150, 10, 140, 40);
        [button setTitle:@"查看更多话题>" forState:UIControlStateNormal];
        button.titleLabel.textAlignment = NSTextAlignmentRight;
        [button setTitleColor:[WebColor steelBlue] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16.0f];
        [button addTarget:self action:@selector(MoreInformationPage:) forControlEvents:UIControlEventTouchUpInside];
        [gray addSubview:button];
        [topic release];
        return gray;
    }else {
        UIView *gray = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height * 3 / 4, self.view.frame.size.width, 60)];
        gray.backgroundColor = [WebColor whiteSmoke];
        
        
        UIImageView *hot = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, 20, 20)];
        hot.image = [UIImage imageNamed:@"hot.png"];
        [gray addSubview:hot];
        [hot release];
        
        UILabel *group = [[UILabel alloc] initWithFrame:CGRectMake(30, 10, gray.frame.size.width / 4, 40)];
        group.text = @"热门小组";
        group.textColor = [UIColor lightGrayColor];
        group.font = [UIFont systemFontOfSize:19];
        [gray addSubview:group];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(self.view.frame.size.width - 150, 10, 150, 40);
        [button setTitle:@"查看更多话题>" forState:UIControlStateNormal];
        [button setTitleColor:[WebColor deepSkyBlue] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:19];
        [button addTarget:self action:@selector(MoreGroupPage:) forControlEvents:UIControlEventTouchUpInside];
        [gray addSubview:button];
        [group release];
        
        return gray;
        
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return [_hotTopicArray count];
    } else {
        return [_hotGroupArray count];
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        HotTopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"firstGroupCell"];
        cell.backgroundColor = [WebColor whiteSmoke];
        HotTopicModel *hotTopic = [_hotTopicArray objectAtIndex:indexPath.row];
        NSString *str = [NSString stringWithFormat:@"%@·%@",[hotTopic.userinfo objectForKey:@"uname"], hotTopic.addtime_f];
        cell.titleLabel.text = hotTopic.title;
        cell.contentLabel.text = hotTopic.content;
        cell.writerLabel.text = str;
//        cell.dateLabel.text = hotTopic.addtime_f;
        NSNumber *num = [hotTopic.counterList objectForKey:@"comment"];
        cell.counterLabel.text = [NSString stringWithFormat:@"%@", num];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }else{
        
        HotGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secondGroupCell"];
        
        cell.backgroundColor = [WebColor whiteSmoke];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return self.view.frame.size.height / 4;
    }else{
        return 280;
    }
}
-(void)startConnect
{
    [AFNetWoringYu netWorkWithURL:@"http://api.pianke.me/group/group_index" body:nil resultBlock:^(id result) {
        
        NSDictionary *JsonDic = [NSDictionary dictionaryWithDictionary:result];
        
        NSDictionary *dataDic = [JsonDic objectForKey:@"data"];
        NSArray *array = [dataDic objectForKey:@"hottopic"];
        
        for (NSDictionary *dic in array) {
            HotTopicModel *TopicModel = [[HotTopicModel alloc] init];
            [TopicModel setValuesForKeysWithDictionary:dic];
            [self.hotTopicArray addObject:TopicModel];
            [TopicModel release];
        }
        
        
        NSArray *secondArray = [dataDic objectForKey:@"hotgroup"];
        for (NSDictionary *secondDic in secondArray) {
            HotGroupModel *GroupModel = [[HotGroupModel alloc] init];
            [GroupModel setValuesForKeysWithDictionary:secondDic];
            [self.hotGroupArray addObject:GroupModel];
            [GroupModel release];
        }
        
        [_groupTableView reloadData];
        
    }];
}

-(void)MoreInformationPage:(id)sender
{
    
}


-(void)MoreGroupPage:(id)sender
{
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
